"""
@author: 江同学呀
@file: boolean_field.py
@date: 2024/7/25 14:28
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/boolean.html

    true and false values.
    true 和 false 值。
"""
from abc import ABC
from typing import Union, Type, Dict, Any

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import DocValuesAttribute, StoreAttribute, BoostAttribute, IndexAttribute, \
    NullValueAttribute, OnScriptErrorAttribute, ScriptAttribute, MetaAttribute
from espc.utils.build_add_attr import build_add_attr


class BooleanField(
    _BaseField, DocValuesAttribute, StoreAttribute, BoostAttribute, IndexAttribute, NullValueAttribute, ScriptAttribute,
    OnScriptErrorAttribute, MetaAttribute, ABC
):
    """
    布尔 字段
    """
    _field_type: str = "boolean"

    def __init__(
            self, field_name: str = None, value: bool = None, default: bool = None, boost: float = None,
            doc_values: bool = None, index: bool = None, null_value: Any = None, on_script_error: str = None,
            script: str = None, store: bool = None, meta: Dict = None
    ):
        super(BooleanField, self).__init__(
            field_name=field_name, value=value, default=default, boost=boost, doc_values=doc_values, index=index,
            null_value=null_value, store=store, meta=meta, on_script_error=on_script_error, script=script
        )
        return

    def __get__(self, instance: _Base, owner: Type["BooleanField"]) -> Union["BooleanField", bool]:
        return super(BooleanField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: bytes):
        super(BooleanField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> bool:
        return self._value

    @value.setter
    def value(self, value: bool):
        self._value: bool = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type,
        }
        return mapping


